package com.yoouce.patchmng.util;

import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * jsch工具类
 * Created by Scott.Lee on 15-10-13.
 */
public class JSchUtil {

    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(JSchUtil.class);
    private String charset = "UTF-8"; // 设置编码格式
    private String user; // 用户名
    private String passwd; // 登录密码
    private String host; // 主机IP
    private JSch jsch;
    private Session session;

    /**
     * @param user   用户名
     * @param passwd 密码
     * @param host   主机IP
     */
    public JSchUtil(String user, String passwd, String host) {
        this.user = user;
        this.passwd = passwd;
        this.host = host;
    }

    /**
     * 连接到指定的IP
     *
     * @throws JSchException
     */
    public void connect() throws JSchException {
        LOG.info("开始连接SCP");
        jsch = new JSch();
        session = jsch.getSession(user, host, 22);
        session.setPassword(passwd);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
    }

    /**
     * 执行相关的命令
     */
    public List<String> execCmd(String command) {
        List<String> result = new ArrayList<String>();

        BufferedReader reader = null;
        Channel channel = null;

        String buf = null;
        try {

            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            channel.connect();
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in,
                    Charset.forName(charset)));

            while ((buf = reader.readLine()) != null) {
                result.add(buf.toString());
                LOG.info(buf);
            }

        } catch (IOException e) {
            result.add(buf.toString());
            e.printStackTrace();
            LOG.error(e.getMessage());

        } catch (JSchException e) {
            result.add(buf.toString());
            e.printStackTrace();
            LOG.error(e.getMessage());
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                result.add(buf.toString());
                e.printStackTrace();
                LOG.error(e.getMessage());
            }
            if (channel != null) {
                channel.disconnect();
                session.disconnect();
                LOG.info("SCP关闭连接");
            }
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        String user = "ycecs";
        String passwd = "ycecs";
        String host = "192.168.1.141";

        JSchUtil demo = new JSchUtil(user, passwd, host);
        demo.connect();
        demo.execCmd("ls");
    }
}
